<!--
 * @Author: 宋杰
 * @Date: 2021-01-06 09:58:39
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-01-17 23:14:58
 * @Description: 一家十一口(猜一字) 机构登录页
-->
<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">机 构</h3>
        <el-button class="returnHome" @click="returnHome">返 回</el-button>
      </div>

      <div class="conbox">
        <!-- 用户名 -->
        <el-form-item prop="username" class="formbox">
          <div class="iconbox">
            <img src="../../static/img/user.png" alt="" />
          </div>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <!-- 机构 、学校 -->
        <el-form-item prop="username" class="formbox">
          <div class="iconbox">
            <img src="../../static/img/more.png" alt="" />
          </div>
          <el-select v-model="loginForm.value">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password" class="formbox">
          <div class="iconbox">
            <img src="../../static/img/password.png" alt="" />
          </div>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <div class="show-pwd iconbox" @click="showPwd">
            <img
              :src="
                passwordType === 'password'
                  ? require('../../static/img/open.png')
                  : require('../../static/img/open2.png')
              "
            />
          </div>
        </el-form-item>
      </div>

      <el-button
        type="primary"
        style="width: 30%; margin-bottom: 30px; margin-left: 72px"
        @click="login(2)"
        >登录</el-button
      >
      <el-button
        type="primary"
        style="width: 30%; margin-bottom: 30px"
        @click="register"
        >注册</el-button
      >

      <div class="tips">
        <span style="color: #2276c3;"> 忘记密码</span>
      </div>
    </el-form>

    <!-- footer -->
    <div class="footers">
      <div class="footer-center">
        版权所有©江苏省建设教育协会 地址：南京市南通路118号茂悦港5号楼8楼
        邮编：210036 苏ICP备17047788号
      </div>
    </div>
  </div>
</template>

<script>
// import { validUsername } from "@/utils/validate";
import { adminRoutes, institutionRoutes } from "@/router/index.js";
export default {
  name: "Login",
  data() {
    // 用户名提示语
    const validateUsername = (rule, value, callback) => {
      //   if (!validUsername(value)) {
      //     callback(new Error("请输入正确的用户名"));
      //   } else {
      //     callback();
      //   }
    };
    // 密码提示语句
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位数字"));
      } else {
        callback();
      }
    };
    // 机构性质选择提示
    const validateSchoolname = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请选择机构"));
      } else {
        callback();
      }
    };
    return {
      // showLoading: true, // 显示loading
      imgUrl: require("../../static/img/jigou.png"), // 背景图片地址
      // 机构性质选择
      options: [
        {
          value: "1",
          label: "学校"
        },
        {
          value: "2",
          label: "培训机构"
        },
        {
          value: "3",
          label: "企业单位"
        },
        {
          value: "4",
          label: "学会"
        },
        {
          value: "5",
          label: "培训中心"
        },
        {
          value: "6",
          label: "协会"
        }
      ],

      // 管理员登录的参数
      loginForm: {
        username: "新东方培训机构", //用户名
        password: "111111", //密码
        value: "1" // 选择机构性质得到的值
      },
      // 表单验证的功能
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ],
        value: [
          { required: true, trigger: "blur", validator: validateSchoolname }
        ]
      },
      // 加载
      loading: false,
      // 查看密码
      passwordType: "password",
      // 转跳页面用的
      redirect: undefined
    };
  },
  watch: {
    // 在后台登录系统的登陆界面见过这几行代码：
    // 因为有immediate: true 说明加载登录页面时就会执行handler函数，handler的第一个参数表示to,
    // 所以上述代码的意义就是，加载登录页后，直接获取，登录页地址栏的的redirect参数，赋值给this.redirect
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    // 转跳到 "管理员首页" 或 "机构用户首页"
    login(val) {
      // debugger
      console.log("loginForm", this.loginForm);
      if (val == 1) {
        this.$router.addRoutes(adminRoutes);
        this.$router.push({ name: "assessmentAudit" });
      } else {
        this.$router.addRoutes(institutionRoutes);
        this.$router.push({ name: "HomePage" });
      }
    },

    // 转跳到 "注册页"
    register() {
      this.$router.push({ name: "registered" });
    },

    // 点击查看密码
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },

    // 返回按钮
    returnHome() {
      this.$router.push({ name: "touristsinfo" });
    }

    // 监听回车键 调用的登录
    // handleLogin() {
    //   this.$refs.loginForm.validate(valid => {
    //     if (valid) {
    //       this.loading = true;
    //       this.$store
    //         .dispatch("user/login", this.loginForm)
    //         .then(() => {
    //           if (this.show == 1) {
    //             this.$router.addRoutes(adminRoutes);
    //           } else {
    //             this.$router.addRoutes(institutionRoutes);
    //           }
    //           debugger;
    //           this.$router.push({ path: this.redirect || "/" });
    //           this.loading = false;
    //         })
    //         .catch(() => {
    //           this.loading = false;
    //         });
    //     } else {
    //       console.log("error submit!!");
    //       return false;
    //     }
    //   });
    // },
  },
  mounted() {}
};
</script>
<style scoped>
.login-container .el-input input {
  color: #000;
}
/* input盒子 */
.el-input {
  display: inline-block;
  height: 47px;
  width: 80%;
  display: flex;
  align-items: center;
}
input {
  background: transparent;
  border: 0px;
  -webkit-appearance: none;
  border-radius: 0px;
  padding: 12px 5px 12px 15px;
  color: #000;
  height: 47px;
  caret-color: #000;
}
.el-input > :nth-child(1) {
  border: none;
}

input:-webkit-autofill {
  box-shadow: 0 0 0px 1000px #283443 inset !important;
  -webkit-text-fill-color: #000 !important;
}
/* 输入框父盒子 */
.el-form-item {
  border: 1px solid rgba(200, 200, 200, 0.3);
  background: #fff;
  border-radius: 5px;
  color: #454545;
}
.el-form-item > :nth-child(1) {
  display: flex;
  align-items: center;
}
/* 学校选择 */
.el-select {
  width: 100%;
}
.el-select > :nth-child(1) > :nth-child(1) {
  border: none !important;
}
/* 学校选择end */

/* 图片盒子 */
.iconbox {
  display: flex;
  align-items: center;
}
.iconbox > img {
  width: 20px;
  margin: 0 10px;
}
.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  z-index: 1;
  background: url("../../static/img/jigou.png") no-repeat;
  background-size: 100% 70%;
}

/* 登录方块的最外层div */
.login-form {
  position: absolute;
  top: calc(50% - 250px);
  left: calc(50% - 250px);
  width: 500px;
  height: 500px;
  max-width: 100%;
  z-index: 3;
  padding: 0 35px;
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
  background-color: #ffffff;
  box-shadow: 0px 0px 30px 10px #abcdef70;
}
.conbox {
  min-height: 45%;
}
.tips {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  font-size: 14px;
  color: #000;
  margin-bottom: 10px;
}
.tips > span {
  cursor: pointer;
}
.tips > span:first-of-type {
  margin-right: 16px;
}
.title-container {
  position: relative;
  margin-top: 15%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.title {
  font-size: 16px;
  color: #000;
  margin: 0px auto 40px auto;
  text-align: center;
  font-weight: bold;
  border-bottom: 0px solid #1e486f59;
}

.title.is-active {
  color: #409eff;
  border-bottom: 2px solid #409eff;
}
.footers {
  position: fixed;
  bottom: 0;
  background-color: #093b69;
  text-decoration: none;
  width: 100%;
  z-index: 999;
}
.footer-center {
  float: none;
  margin: 0;
  color: #fff;
  text-align: center;
  text-decoration: none;
  padding: 0.5% 0;
  line-height: 30px;
  font-size: 12px;
  vertical-align: middle;
}
/* 返回首页按钮 */
.returnHome {
  position: absolute;
  top: -50px;
  left: -20px;
  padding: 10px 15px;
  /* border: none !important; */
  font-size: 16px;
  opacity: 0.8;
}
/* 返回首页按钮 */
</style>
